package com.gaojiawei.multiThread18_CompletableFutureTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 任务a和任务b并行执行，任务c在任务a和任务b都执行完了之后再执行
 * thenCombine thenAcceptBoth thenRunAfter 同理区别在于获不获取结果，返不返回结果。
 */
public class L06_ThenCombineThenAcceptBothRunAfterBoth {

    public static void main(String[] args) {
        testThenCombine();

    }

    public static void testThenCombine(){

        //自定义线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        CompletableFuture.supplyAsync(()->{
           System.out.println("begin a");
           try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println("finish a");
           return "task a is ok";
        },fixedThreadPool).thenCombine(CompletableFuture.supplyAsync(()->{
           System.out.println("begin b");
           try {
               Thread.sleep(5000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println("finish b");
           return "task b is ok";
       },fixedThreadPool),(resA,resB)->{
           System.out.println("begin c");
           System.out.println("finish c");
           return resA+resB;
       });


    }
}
